<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ERROR);
const ROOT_DIR = __DIR__;
const ASSETS_DIR = ROOT_DIR . "/assets";
const CONTROLLER_DIR = ROOT_DIR . "/controller";
$controller = filter_input(INPUT_GET, "c", FILTER_SANITIZE_STRING);
$action = filter_input(INPUT_GET, "a", FILTER_SANITIZE_STRING);
$controller_class = ucfirst($controller);
$controller_file = CONTROLLER_DIR . "/" . $controller_class . ".php";
if (!file_exists($controller_file)) {
  echo "The controller " . $controller_class . " does not exist.";
  return;
}
require_once $controller_file;
$controller_class = "Controller\\" . $controller_class;
$controller = new $controller_class();
if(!method_exists($controller, $action)) {
  echo "The action " . $action . " does not exist.";
  return;
}
$args = [];
foreach(getMethodArguments($controller, $action) as $argument) {
  $args[] = filter_input(INPUT_GET, $argument->getName());
}
$controller->$action(...$args);


function getMethodArguments($controller, $action) {
  try {
    $reflection = new ReflectionMethod($controller, $action);
  } catch(ReflectionException $e) {
    return [];
  }
  return $reflection->getParameters();
}